package com.sky.service.impl;

import com.sky.constant.StatusConstant;
import com.sky.entity.Orders;
import com.sky.mapper.*;
import com.sky.service.WorkSpaceService;
import com.sky.vo.BusinessDataVO;
import com.sky.vo.DishOverViewVO;
import com.sky.vo.OrderOverViewVO;
import com.sky.vo.SetmealOverViewVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;

@Service
public class WorkSpaceServiceImpl implements WorkSpaceService {

    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetMealMapper setMealMapper;
    @Autowired
    private UserMapper userMapper;

    /**
     * 查询订单管理数据
     * @return
     */
    @Override
    public OrderOverViewVO getOrderOverView() {
        LocalDate now = LocalDate.now();
        LocalDateTime of = LocalDateTime.of(now, LocalTime.MIN);
        LocalDateTime of1 = LocalDateTime.of(now, LocalTime.MAX);
//        已完成
        Integer completedOrders=orderMapper.findOrdersOrSTATUS(of,of1, Orders.COMPLETED);
//        已取消
        Integer cancelledOrders = orderMapper.findOrdersOrSTATUS(of, of1, Orders.CANCELLED);
//        全部
        Integer allOrders = orderMapper.findOrdersOrSTATUS(of, of1, null);
//        待接单
        Integer waitingOrders = orderMapper.findOrdersOrSTATUS(of, of1, Orders.TO_BE_CONFIRMED);
//        带派送
        Integer deliveredOrders = orderMapper.findOrdersOrSTATUS(of, of1, Orders.CONFIRMED);

        return  OrderOverViewVO.builder()
                .completedOrders(completedOrders)
                .cancelledOrders(cancelledOrders)
                .allOrders(allOrders)
                .waitingOrders(waitingOrders)
                .deliveredOrders(deliveredOrders)
                .build();
    }

    /**
     * 查询菜品总览
     * @return
     */
    @Override
    public DishOverViewVO getDishOverView() {
//        起售
         Integer x= dishMapper.findStatusDish(StatusConstant.ENABLE);
//         停售
         Integer xx= dishMapper.findStatusDish(StatusConstant.DISABLE);
         return  DishOverViewVO.builder()
                 .sold(x)
                 .discontinued(xx)
                 .build();
    }

    /**
     * 查询套餐总览
     * @return
     */
    @Override
    public SetmealOverViewVO getSetmealOverView() {
        Integer qisho= setMealMapper.findStatusSETMEAL(StatusConstant.ENABLE);
        Integer tingshou= setMealMapper.findStatusSETMEAL(StatusConstant.DISABLE);
        return SetmealOverViewVO.builder()
                .sold(qisho)
                .discontinued(tingshou)
                .build();
    }

    /**
     * 工作台今日数据查询
     * @param begin
     * @param end
     * @return
     */
    @Override
    public BusinessDataVO getBusinessData(LocalDateTime begin, LocalDateTime end) {
        /**  按顺序 就是了
         * 营业额：当日已完成订单的总金额
         * 有效订单：当日已完成订单的数量
         * 订单完成率：有效订单数 / 总订单数
         * 平均客单价：营业额 / 有效订单数
         * 新增用户：当日新增用户的数量
         */
//        营业额
        Double turnover=orderMapper.findsumMoney(begin,end,Orders.COMPLETED);
        turnover = turnover==null ? 0.0:turnover;
//        有效订单
        Integer validOrderCount = orderMapper.findOrdersOrSTATUS(begin, end, Orders.COMPLETED);
//        总订单
        Integer amountOrder = orderMapper.findOrdersOrSTATUS(begin, end,null);
//        订单完成率
        Double orderCompletionRate =0.0;
        Double unitPrice = 0.0;
        if(amountOrder!=0 && validOrderCount!=0) {
            orderCompletionRate= validOrderCount.doubleValue()/amountOrder;
//            平均客单价
            unitPrice= turnover/validOrderCount;
        }
//          新增用户
        Integer newUsers=userMapper.findUser(begin,end);

        return BusinessDataVO.builder()
                .turnover(turnover)
                .validOrderCount(validOrderCount)
                .orderCompletionRate(orderCompletionRate)
                .unitPrice(unitPrice)
                .newUsers(newUsers)
                .build();
    }
}
